<?php
namespace Admin\Controller;
use Think\Controller;
use Admin\Controller\CommonController;
class GroupController extends CommonController {
	public function index() {
        $groupname = I('get.groupname', '');
        $map = array();
        if($groupname) {
            $map['groupname'] = $groupname;
        }

        $groupModel = M('UserGroup');
        $total = $groupModel->where($map)->count();
        $page = new \Think\Page($total, 25);
        $show = $page->show();

        $groupList = $groupModel->where($map)
                     ->limit($page->firstRow .',' . $page->listRows)
                     ->select();


        $this->assign('showpage', $show);
        $this->assign('groupList', $groupList);
		$this->display();
	}

	public function addGroup() {
		if(IS_POST) {
			$groupModel = D('UserGroup');
			if($groupModel->create()) {
				$res = $groupModel->add();
				if($res) {
					$this->success('添加成功');
				} else {
					$this->error('添加失败，请重试');
				}
			} else {
				$this->error($groupModel->getError());
			}

		} else {
			$this->error('非法操作');
		}		
	}

	public function editGroup() {
		if(IS_POST) {
			$groupModel = D('UserGroup');
			if($groupModel->create()) {
				$res = $groupModel->save();
				if($res !== false) {
					$this->success('修改成功');
				} else {
					$this->error('修改失败，请重试');
				}
			} else {
				$this->error($groupModel->getError());
			}

		} else {
			$this->error('非法操作');
		}		
	}

	/**
	 * 删除后台用户
	 * @return [type] [description]
	 */
	public function ajaxDelGroup() {
		if(IS_GET) {
			$id = I('get.groupid');
		} elseif(IS_POST) {
			$id = I('post.groupid');
			is_array($id) && $id = implode(',', $id);
		}

		if(empty($id)) {
			$message['status'] = -1;
			$message['msg'] = '选项不能为空';
			$this->ajaxReturn($message);
		}

		$map['groupid'] = array('in', $id);
		$res = M('UserGroup')->where($map)->delete();
		if($res) {
			$message['status'] = 1;
			$message['msg'] = '删除成功';
			$this->ajaxReturn($message);			
		} else {
			$message['status'] = -1;
			$message['msg'] = '删除失败，请重试';
			$this->ajaxReturn($message);
		}
	}

	public function ajaxGetGroupInfo($groupid = 0) {
        $groupModel = M('UserGroup');
        if($groupid){
            $groupinfo = $groupModel->where('groupid=' . $groupid)->find();
            if($groupinfo){
                $this->ajaxReturn($groupinfo);
            }else{
                $this->ajaxReturn(0);
            }
        }else{  
            return false;
        }		
	}

	/**
	 * 通过组ID获取权限
	 * @return [type] [description]
	 */
    public function getAuthByGroup() {
    	$groupid = I('get.groupid', 0);
    	$authList = M('UserGroup')->where(array('groupid' => $groupid))->getField('auth');
    	$this->_authTree($authList);
    }


    /**
     * 设置用户自身权限
     * @return [type] [description]
     */
    public function ajaxSetGroupAuth() {
 		$idList=I("get.idList");
        $regx='/^\d+$/';
        foreach(explode(",",$idList) as $v){
            preg_match($regx, $v, $a);
            if(count($a) >= 1){
                $arr[]=$v;
            }
        }

        $idList = implode(",", $arr);

        $a = M('UserGroup')->save(array(
            'auth' => $idList,
            'groupid' => I("get.groupid"),
        ));
        $this->ajaxReturn($a);   	
    }

   /**
     * 权限菜单
     * @param  string $authlist [description]
     * @return [type]           [description]
     */
    protected function _authTree($authlist=""){
        $authlist=explode(",",$authlist);
        $system_channel_model=M('SystemChannel');
        $top_auth_list=$system_channel_model->where(array("spid"=>0))->select();
        $arr['id']=0;
        $arr['item']=array();

        foreach($top_auth_list as $k=>$v){
            $arr['item'][]=array('id'=>"T_".$v['scid'],'child'=>'1','text'=>$v['channelname'],'item'=>array());
            $seconde_auth_list=$system_channel_model->where(array("spid"=>$v['scid']))->select();
            foreach($seconde_auth_list as $kk=>$vv){
                $arr['item'][$k]['item'][$kk]=array('id'=>$vv['scid'],'text'=>$vv['channelname']);
                if(in_array($vv['scid'],$authlist)){
                    $arr['item'][$k]['item'][$kk]['checked']="1";
                }
            }
        }
        $this->ajaxReturn($arr);
    }
}